package aam.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import aam.model.MessageListVO;
import aam.model.PagingBean;
import aam.model.member.MemberDao;
import aam.model.member.MemberVO;
import aam.model.member.MessageVO;

@Service
public class MemberServiceImpl implements MemberService {
	@Resource(name="memberDaoImpl")
	private MemberDao memberDao;

	public MemberVO login(MemberVO vo) throws Exception {
		return memberDao.login(vo);
	}
	public MemberVO getMemberInfo(String id) throws Exception {
		return memberDao.getMemberInfo(id);
	}
	@Override
	public String ajaxIdCheck(String id) throws Exception {
		return this.memberDao.ajaxIdCheck(id);
	}
	@Override
	public String ajaxNickCheck(String nick) throws Exception {
		return this.memberDao.ajaxNickCheck(nick);
	}
	@Override
	public int registerMember(MemberVO mvo) throws Exception {
		return this.memberDao.registerMember(mvo);
	}
	//출석 -dw
	@Override
	public boolean checkAttend(String id) throws Exception {
		String result=this.memberDao.SelectcheckAttend(id);
		boolean flag=false;
		if(result==null){
			this.memberDao.checkAttend(id);
			this.memberDao.checkPointUp(id);
			flag=true;
		}
		return flag;
	}
	@Override
	public void updateUserInfo(MemberVO mvo) throws Exception 
	{
		memberDao.updateUserInfo(mvo);
	}
	
	@Override
	public MemberVO updateGrade(MemberVO mvo) throws Exception {
		MemberVO uvo = mvo;
		if(mvo.getGrade().equals("A")||mvo.getGrade().equals("S")){
			memberDao.updateGrade(mvo);
			uvo = memberDao.login(mvo);
	}
		return uvo;
	}
	/** 쪽지*/
	/** 쪽지함으로 수정*/ 
	@Override
	public MessageListVO getMessage(String id,String pageNo) throws Exception {
		int p=1;
		if(pageNo!=null){
			p=Integer.parseInt(pageNo);
		}
		HashMap map=new HashMap();
		map.put("id", id);
		map.put("page", p);
		ArrayList<MessageVO> list=(ArrayList<MessageVO>) this.memberDao.getMessage(map);
		PagingBean pb=new PagingBean(p,this.memberDao.getMessageTotalConunt(id));
		return new MessageListVO(list,pb);
	}
	@Override
	public MessageListVO sendMessage(String id,String pageNo) throws Exception {
		int p=1;
		if(pageNo!=null){
			p=Integer.parseInt(pageNo);
		}
		HashMap map=new HashMap();
		map.put("id", id);
		map.put("page", p);
		ArrayList<MessageVO> list=(ArrayList<MessageVO>) this.memberDao.sendMessage(map);
		PagingBean pb=new PagingBean(p,this.memberDao.sendMessageTotalConunt(id));
		return new MessageListVO(list,pb);
	}
	@Override
	public String ajaxMessageFindByNick(String nick) throws Exception {
		return this.ajaxMessageFindByNick(nick);
	}
	@Override
	public void forwardingMessage(String sendId, String content, String getId)
			throws Exception {
		HashMap map=new HashMap();
		map.put("sendId", sendId);
		map.put("content", content);
		map.put("getId", getId);
		this.memberDao.forwardingMessage(map);
	}
	@Override
	public MessageVO getMessageInfo(int no) throws Exception {
		return this.memberDao.getMessageInfo(no);
	}
	@Override
	public MessageVO sendMessageInfo(int no) throws Exception {
		return this.memberDao.sendMessageInfo(no);
	}
	@Override
	public int deleteGetMessage(int no) throws Exception {
		MessageVO vo=this.memberDao.getDeleteCheck(no);
		int result;
		if(vo==null||vo.getGetVO().getId().equals(vo.getSendVO().getId())){
			result=this.memberDao.deleteMessage(no);
		}else{
			result=this.memberDao.getDeleteCheckUpdate(no);
		}
		return result;
	}
	@Override
	public int deleteSendMessage(int no) throws Exception {
		MessageVO vo=this.memberDao.sendDeleteCheck(no);
		int result;
		if(vo==null||vo.getGetVO().getId().equals(vo.getSendVO().getId())){
			result=this.memberDao.deleteMessage(no);
		}else{
			result=this.memberDao.sendDeleteCheckUpdate(no);
		}
		return result;
	}
}
